package com.dameng.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dameng.system.entity.SystemDept;
import com.dameng.system.service.SystemDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/systemDept")
public class SystemDeptController {

    @Resource
    SystemDeptService systemDeptService;

    @GetMapping("/manager")
    public String manager() {
        return "/system/Dept/manager";
    }

    @PostMapping("/deptList")
    @ResponseBody
    public List<SystemDept> deptList() {
        return systemDeptService.list();
    }

    @PostMapping("/list")
    @ResponseBody
    public Page<SystemDept> list(int page, int limit, SystemDept systemDept) {
        QueryWrapper<SystemDept> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(systemDept.getPid())) {
            wrapper.lambda().eq(SystemDept::getPid, systemDept.getPid());
        }
        if (StringUtils.isNotEmpty(systemDept.getName())) {
            wrapper.lambda().like(SystemDept::getName, systemDept.getName());
        }
        Page<SystemDept> page1 = new Page<>(page, limit);
        return systemDeptService.page(page1, wrapper);
    }

    @GetMapping("/add")
    public String add(ModelMap model, SystemDept systemDept) {
        model.addAttribute("systemDept", systemDept);
        model.addAttribute("pName", systemDeptService.getById(systemDept.getPid()).getName());
        model.addAttribute("type", "save");
        return "/system/Dept/edit";
    }

    @PostMapping("/save")
    @ResponseBody
    @Transactional
    public Boolean save(SystemDept systemDept) {
        return systemDeptService.save(systemDept);
    }

    @GetMapping("/edit")
    public String edit(ModelMap model, String deptId) {
        SystemDept systemDept = systemDeptService.getById(deptId);
        model.addAttribute("systemDept", systemDept);
        model.addAttribute("pName", systemDeptService.getById(systemDept.getPid()).getName());
        model.addAttribute("type", "update");
        return "/system/Dept/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    @Transactional
    public Boolean update(SystemDept systemDept) {
        return systemDeptService.updateById(systemDept);
    }

    @PostMapping("/delete")
    @ResponseBody
    @Transactional
    public Boolean delete(String... deptIds) {
        List<String> ids = Arrays.asList(deptIds);
        List<String> idList = new ArrayList<>();
        for (String deptId : ids) {
            List<String> depts = systemDeptService.childDeptByDeptId(deptId);
            idList.add(deptId);
            idList.addAll(depts);
        }
        return systemDeptService.removeByIds(idList);
    }

}
